package com.yfbao.horizon.compress;

import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HessianSerialization {

    // 将对象序列化为字节数组
    public static byte[] serializeObject(Object obj) {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            HessianOutput out = new HessianOutput(bos);
            out.writeObject(obj);
            out.close();
            return bos.toByteArray();
        }catch (IOException e){

        }
       return null;
    }

    // 将字节数组反序列化为对象
    public static Object deserializeObject(byte[] data)  {
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(data);
            HessianInput in = new HessianInput(bis);
            Object obj = in.readObject();
            in.close();
            return obj;
        }catch (IOException e){
        }
        return null;
    }

    public static void main(String[] args) throws IOException {
        // 创建一个 Person 对象
        Map<String,Object> map =new HashMap<>();
        List<String> list =new ArrayList<>();
        list.add("14737506170");
        list.add("18435088260");
        map.put("aaaaa",list);
        // 将 Person 对象序列化为字节数组
        byte[] serializedData = serializeObject(map);

        // 将字节数组反序列化为对象
        Map deserializedPerson = (Map) deserializeObject(serializedData);

        Object aaaaa = map.get("aaaaa");
        System.out.println((List<String>)aaaaa);
        // 输出反序列化后的对象
        System.out.println("Deserialized Person: " + deserializedPerson);
    }

}
